package ru.mcfr.oxygen.framework.operations;

import ro.sync.ecss.extensions.api.ArgumentDescriptor;
import ro.sync.ecss.extensions.api.AuthorOperationException;
import ro.sync.ecss.extensions.api.node.AuthorNode;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.text.BadLocationException;
import java.io.*;
import java.net.URISyntaxException;

/**
 * Created by IntelliJ IDEA.
 * User: ws
 * Date: 28.06.11
 * Time: 11:28
 * To change this template use File | Settings | File Templates.
 */
public class JavaScriptOperation extends McfrBaseAuthorOperation {
    {
        description = "JS операция";

        argumentsNameArray = new String[]{"скрипт", "параметры"};

        argumentsTypesArray = new Object[]{ArgumentDescriptor.TYPE_STRING, ArgumentDescriptor.TYPE_STRING};

        argumentsDescriptionsArray = new String[]{"имя файла в каталоге script фреймверка", "параметры запуска скрипта в формате param=value param2=value2"};
    }

    @Override
    public void doMainOperation() {

        String jsFolder = null;
        try {
            jsFolder = JavaScriptOperation.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return;
        }
        String folder = new File(jsFolder).getParentFile().getPath() + File.separator + "js" + File.separator;
        final String fileName = folder + argumentsValues.get(argumentsNameArray[0]);
        StringWriter swError = new StringWriter();
        StringWriter swStdOut = new StringWriter();
        ScriptEngineManager factory = new ScriptEngineManager();
        final ScriptEngine engine = factory.getEngineByName("JavaScript");

        engine.put("argumentsValues", this.argumentsValues);
        engine.put("selectedElement", this.selectedElement);
        engine.put("selectedFragment", this.selectedFragment);
        engine.put("argumentsMap", this.argumentsMap);
        engine.put("controller", this.controller);
        engine.put("authorAccess", this.authorAccess);
        engine.put("AuthorNode", AuthorNode.class);
        engine.put("globalObject", this);

        engine.getContext().setErrorWriter(swError);
        engine.getContext().setWriter(swStdOut);

        Thread jsRunner = new Thread() {
            public void run() {
                try {
                    engine.eval(new FileReader(fileName));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ScriptException e) {
                    authorAccess.getWorkspaceAccess().showErrorMessage(e.getMessage());
                }
            }
        };
        jsRunner.setDaemon(true);
        jsRunner.run();
    }

    @Override
    public void doOperationWithSelected
            () throws AuthorOperationException, BadLocationException {
    }

    @Override
    public void doOperationAtCaretPos
            () throws AuthorOperationException {
    }
}
